// __PUBLIC__/js/page/admin/global/area
require(['../../common'], function() {
  require(['global.area'])
})

define('global.area', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();

  function del(selected) {
    // sql where in
    $.post(APP + '?app=admin&m=Global&a=doDeleteArea', {
      ids: selected.id.join()
    })
      .done(function(data) {
        if (data == 1) {
          mygrid.del(selected.index)
        } else {
          alert(lang.delFails)
        }
      })
  }

  $table
  // 打开子级
  .on('click', '.A_sub', function(e) {
    e.preventDefault()
    var id = mygrid.getCurrent(this).id
    // 先检验是否有子级
    // yan: 如果没有子级，模板中不应该给链接
    $.post(APP + '?app=admin&m=Global&a=ajaxQryAreaCount', {
      pid: id
    })
      .done(function(res) {
        if (res > 0) {
          var url = APP + '?app=admin&m=Global&a=doQryArea&pid=' + id
          location.href = url
        } else {
          e.preventDefault()
        }
      })
  })
  // 删除当前行
  .on('click', '.A_del', function(e) {
    e.preventDefault()
    var current = mygrid.getCurrent(this)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    del(selected)
  })

  // 删除选中行
  $('#J_del').on('click', function(e) {
    e.preventDefault()
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    del(selected)
  })
})
